﻿using System.Data;
using Marbles.Core.Connectors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.BBS;
using Marbles.Core.Authentication;


namespace Marbles.Web.BBS
{
    public partial class ForumPost : Marbles.Core.Content.UserControl
    {
        public string ForumName { get { object obj = ViewState["ForumName"]; return (obj == null) ? "" : obj.ToString(); } set { ViewState["ForumName"] = value; } }
        public Int64 PostId { get { object obj = ViewState["PostId"]; return (obj == null) ? 0 : Convert.ToInt64(obj); } set { ViewState["PostId"] = value; } }

        public event EventHandler PostCreated;
        public event EventHandler PostCancelled;

        private Core.BBS.ForumPost parentpost = null;
        private Core.BBS.Forum forum = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (forum == null) { forum = new Core.BBS.Forum(this.CurrentWeb.SiteId, ForumName); }
            bool isEditor = this.Page.IsEditor || this.Page.IsAdministrator;
            pnlOpts.Visible = cChkLocked.Visible = cChkSticky.Visible = isEditor;

        }

        public void Clear()
        {
            if (forum == null) { forum = new Core.BBS.Forum(this.CurrentWeb.SiteId, ForumName); }
            cLnkCategory.Text = forum.CategoryTitle;
            cLnkTitle.Text = forum.Title;
            cLnkTitle.NavigateUrl = "Forum.aspx?f=" + ForumName;
            cLnkCategory.NavigateUrl = "Default.aspx";
            cLblDescription.Text = forum.Description;
            bool isEditor = this.Page.IsEditor || this.Page.IsAdministrator;
            cTxtAuthor.Text = string.Empty;
            cTxtBody.Text = string.Empty;
            cTxtSubject.Text = string.Empty;
            cTxtAuthor.Text = this.Page.Username.Trim();
            cTxtAuthor.Enabled = string.IsNullOrEmpty(cTxtAuthor.Text);
            pnlOpts.Visible = cChkLocked.Visible = cChkSticky.Visible = isEditor;

            if (this.PostId > 0)
            {
                parentpost = new Core.BBS.ForumPost(this.CurrentWeb.SiteId, this.ForumName, this.PostId);
                cTxtSubject.Text = "RE: " + parentpost.Title;
            }
        }

        private void Initialise()
        {

            forum = new Core.BBS.Forum(this.CurrentWeb.SiteId, ForumName);
            cLnkCategory.Text = forum.CategoryTitle;
            cLnkTitle.Text = forum.Title;
            cLnkTitle.NavigateUrl = "Forum.aspx?f=" + ForumName;
            cLnkCategory.NavigateUrl = "Default.aspx";
            cLblDescription.Text = forum.Description;

            if (this.PostId > 0)
            {
                parentpost = new Core.BBS.ForumPost(this.CurrentWeb.SiteId, this.ForumName, this.PostId);
                cTxtSubject.Text = "RE: " + parentpost.Title;
            }
        }

        protected void cBtnCancel_Click(object sender, EventArgs e)
        {
            if (PostCancelled != null) { PostCancelled(this, EventArgs.Empty); }
        }

        protected void cBtnPost_Click(object sender, EventArgs e)
        {
            Initialise();
            Core.BBS.ForumPost post = null;

            if (parentpost != null)
            {
                post = Core.BBS.ForumPost.CreateNew(forum, parentpost);
            }
            else
            {
                post = Core.BBS.ForumPost.CreateNew(forum);
            }
            post.Author = cTxtAuthor.Text;
            post.Title = cTxtSubject.Text;
            post.Body = cTxtBody.Text;
            post.Sticky = cChkSticky.Checked;
            post.Locked = cChkLocked.Checked;
            post.Insert();
            if (PostCreated != null) { PostCreated(this, EventArgs.Empty); }
            //GoBack();
        }


    }

}